﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model.battle;

namespace com.lover.astd.common.logicexe.temp
{
    public class CampaignExe : TempExeBase, ITempExe
    {
        private int _fromId = 0;
        private int _targetCount = 0;
        private int _doneCount = 0;
        private List<CampaignItem> _campaigns;
        private CampaignItem _todoCampaign;
        public CampaignExe() { _name = ConfigStrings.S_Temp; _readable = ConfigStrings.SR_Temp; }
        public override long execute()
        {
            CampaignMgr mgr = ServiceFactory.getCampaignManager();
            if (_campaigns == null || _campaigns.Count == 0)
            {
                _campaigns = mgr.getCampaigns(_proto, _logger);
                if (_campaigns == null)
                {
                    logInfo("获取战役失败, 将重试, 如果一直出现, 请停止临时任务");
                    return immediate();
                }
            }
            // 判断做哪个战役
            _todoCampaign = null;
            foreach (CampaignItem cam in _campaigns)
            {
                if (cam.id >= _fromId && cam.times < 100) { _todoCampaign = cam; break; }
            }
            if (_todoCampaign == null) return next_halfhour();
            int exe_result = mgr.startCampaign(_proto, _logger, _todoCampaign);
            if (exe_result == 1) return next_halfhour();
            else if (exe_result == 2) { } // 正在战役中, 不用处理, 直接完成战役就好
            else if (exe_result == 3) { return _user.TokenCdTime; }
            else if (exe_result == 4) { return next_hour(); }
            else if (exe_result == 5) { return next_hour(); }
            else if (exe_result == 6) { return next_hour(); }
            else if (exe_result == 7) { return next_hour(); }
            else if (exe_result == 10) { return next_hour(); }
            _doneCount++;
            return immediate();
        }
        #region ITempExe 成员

        public bool isFinished()
        {
            return _doneCount >= _targetCount;
        }

        public void setTarget(Dictionary<string, string> conf)
        {
            if (conf == null || !conf.ContainsKey("count")) return;
            Int32.TryParse(conf["count"], out _targetCount);
            if (conf.ContainsKey("from"))
            {
                Int32.TryParse(conf["from"], out _fromId);
            }
        }

        public string getStatus()
        {
            return String.Format("单人战役 {0}/{1}", _doneCount, _targetCount); 
        }

        #endregion
    }
}
